// pages/comments/comments.js
let app = getApp();
import api from '../../../utils/api.js'
import { HTTP } from '../../../utils/http.js'
import { config } from '../../../utils/config'
import utils from "../../../utils/util"
let http = new HTTP()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: config.imgUrl,
    pics: [],
    src1 : config.imgUrl+'/images/index/xing1.png',
    src2 : config.imgUrl+'/images/index/xing2.png',
    star:{overall_score : 5,quality_score : 5,service_score : 5,},
    score:{allScore :[{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}],vegeScore :[{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}],serviceScore :[{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]},
    inputValue : '',
    picPath : [],
    picList : [],
    canSubmit : true

    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let comproduct = app.globalData.comproduct;
    this.setData({
      comproduct,
      order_no : comproduct.order_no
    })
    
  },
 
 
  /*提交评论*/
  addComment(){
    let that = this;
    if(this.data.canSubmit == false){
      return ;
    }else{
      this.setData({
        canSubmit : false
      })
    }


          
    // if(this.data.inputValue == ''){
    //   wx.showToast({
    //     title: '填写评***价内容',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return 
    // }
    // if( this.data.inputValue  && this.data.inputValue.length < 5){
    //   wx.showToast({
    //     title: '评***价内容过短',
    //     icon: 'none',
    //     duration: 1500
    //   })
    //   return 
    // }
    // 点赞菜品id
   
    // 发送请求
    http.request({
      url: api.integralEvaluate,
      method: 'POST',
      data: {
          order_no: that.data.order_no,
          score: that.data.star.overall_score
      },
      success: (res) => {
         console.log('发布评***价',res);
         if(res.code == 200){
          wx.showToast({
            title: '发布成功',
            icon: 'none',
            duration: 1500
          });

          // 给上一页传递数据
          const pages = getCurrentPages()
          const prevPage = pages[pages.length-2]
          prevPage.setData({
            commentOrderBack : that.data.order_no
          })

          setTimeout(() =>{
            wx.navigateTo({
              url : '/pages/pointsMall/comments/comments?id='+that.data.comproduct.product_id
            })
          },800)
          
          setTimeout(() => {
            app.globalData.comproduct = '';
          }, 1200);
           
         }else if(res.error_code){
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
         }
         
      },
      fail: (res) => {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1500
        });
      },
      complete:() => {
        that.setData({
          canSubmit : true
        })
      }
  })
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
 * 图片放大查看
 */
  previewImg: function (e) {

    var index = e.target.dataset.index;//当前图片地址
    var imgArr = e.target.dataset.list;//所有要预览的图片的地址集合 数组形式
    console.log(index, imgArr)
    wx.previewImage({
      current: imgArr[index],
      urls: imgArr,
    })
  },
  /**
   * 图片上传
   * 
   */

  //上传图片开始
  chooseImg: function (e) {
    var that = this
    let picList =  this.data.picList
    let picPath = this.data.picPath
    if (picList.length < 3) {
      wx.chooseImage({
        count: 3, // 最多可以选择的图片张数，默认9
        sizeType: [ 'compressed'], // original 原图，compressed 压缩图，默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          wx.showLoading({title: '上传中'})
          for (var i = 0; i < tempFilePaths.length; i++) {
            // pics.push(tempFilePaths[i]);
            wx.uploadFile({
              url: config.debug_url+api.uploadFiles, //仅为示例，非真实的接口地址
              filePath: tempFilePaths[i],
              name: 'file',
              formData: {
                // token : wx.getStorageInfoSync('token')
              },
              success (res){
                
                if(res.statusCode == 200 ){
                  let result =JSON.parse(res.data);
                  // console.log('result',result.data.img ,result.data.path);

                  if(result.data.img && result.data.path){
                    picList.push(result.data.img);  //前台展示
                    picPath.push(result.data.path); //传给后台
                    that.setData({
                      picList,picPath
                    })
                  }else{
                    wx.showToast({
                      title: '上传错误',
                      icon: 'none',
                      duration: 1500
                    });
                  }

                }
              },
              fail(){

              },
              complete(){
                wx.hideLoading();
              }

            })
          }
          

          // console.log(pics);
          
        },
      });
    } else {
      wx.showToast({
        title: '最多上传3张图片',
        icon: 'none',
        duration: 1500
      });

    }
  },
  // 删除图片
  deleteImg: function (e) {
    var that = this;
    var picList = this.data.picList;
    var picPath = this.data.picPath
    var index = e.currentTarget.dataset.index;
    picList.splice(index, 1);
    picPath.splice(index, 1)
    this.setData({
      picPath, picList
    })
  },
  // 预览图片
  previewImg1: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var picList = this.data.picList;
    wx.previewImage({
      //当前显示图片
      current: picList[index],
      //所有图片
      urls: picList
    })
  },

  select(e) {
    // console.log(e)
    let starId = e.currentTarget.dataset.index;
    let score = e.currentTarget.dataset.score;
    let star = this.data.star;
    star[score] = starId;
    this.setData({
      star
    })
    
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.jump){
      wx.navigateBack({
        delta : 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})